<template>
  <div id="for-deposit">
    <!-- 缴纳保证金首页 -->
    <div class="deposit-content">
      <div class="top-bg">
        <van-nav-bar class="header" fixed :border="false" @click-left="diffBack">
          <div class="left" slot="left">
            <van-icon name="arrow-left" size="24px" color="#fff"/>
          </div>
          <div class="title" slot="title">{{title}}</div>
        </van-nav-bar>
      </div>
      <div class="reset-wrapper">
        <div class="reset-content bgwrite">
          <div class="reset-top">
            <div class="top-content">
              <div class="has-account" v-if="bankInfo == 1" @click="chooseAccount">
                <div class="img-case">
                  <img src="@/assets/img/public/commonBank.png" alt>
                </div>
                <div class="account-case">{{bankCardObj.bankCardNumber}}</div>
                <div class="name-case">{{bankCardObj.bankName?bankCardObj.bankName:'银行卡'}}</div>
                <div class="right-case">
                  <img src="@/assets/img/performance/personalCenter/rightarrow.png" alt>
                </div>
              </div>
              <div class="no-account" v-if="bankInfo == 0" @click="chooseAccount">
                <div class="tips-case">请选择提现账户</div>
                <div class="right-case">
                  选择
                  <img src="@/assets/img/performance/personalCenter/rightarrow.png" alt>
                </div>
              </div>
            </div>
          </div>
          <div class="reset-bottom">
            <div class="bottom-top">
              <div class="top-header">提现金额</div>
              <div class="top-num">
                <div>￥</div>
                <input
                  type="number"
                  v-model="inputValue"
                  ref="focusContent"
                  @click="inputFocus"
                  @input="handleKey"
                >
              </div>
            </div>
            <div class="bottom-bottom">
              <div class="all-num">
                <div class="left-case">可提现余额 ￥{{walletWithdraw}}</div>
                <div class="right-case" @click="withdrawAll">全部提现</div>
              </div>
              <div class="btn-case" @click="submitWithdraw">提现</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <van-popup v-model="accountShow" position="bottom" :overlay="true">
      <div class="account-list">
        <div class="header-case">
          <div class="mid-header">选择提现账户</div>
          <div class="close-btn" @click="closeDialog">
            <img src="@/assets/img/public/clear.png" alt>
          </div>
        </div>
        <div class="reset-content">
          <!-- w微信 支付宝列表  固定 -->
          <!-- <div class="ali-wechat">
                    <div class="each-case">
                        <div class="key-case">
                            <div class="img-case"><img src="@/assets/img/public/wechat.png" alt=""></div>
                            <div class="key-name">微信账户</div>
                        </div>
                        <div class="value-case">
                            <div class="name-case">AJJSJSJJSJSJSJJSJSJSJSJSJJSJSSSSSSSSSSSSSSSS</div>
                            <div class="right-arrow"><img src="@/assets/img/performance/personalCenter/rightarrow.png" alt=""></div>
                        </div>
                    </div>
                    <div class="each-case">
                        <div class="key-case">
                            <div class="img-case"><img src="@/assets/img/public/alipay.png" alt=""></div>
                            <div class="key-name">支付宝账户</div>
                        </div>
                        <div class="value-case">
                            <div class="name-case">AJJSJSJJSJSJSJJSJSJSJSJSJJSJSSSSSSSSSSSSSSSS</div>
                            <div class="right-arrow"><img src="@/assets/img/performance/personalCenter/rightarrow.png" alt=""></div>
                        </div>
                    </div>
          </div>-->
          <div class="has-add">
            <!-- 所有银行卡列表 除开是支付宝微信-->
            <div class="has-case">
              <div class="each-case" v-for="(itemL,indexL) in list" :key="indexL">
                <div class="key-case" @click="chooseeACard(itemL.uid)">
                  <div class="img-case">
                    <img src="@/assets/img/public/commonBank.png" alt>
                  </div>
                  <div class="key-name">{{itemL.bankName?itemL.bankName:'银行卡'}}</div>
                </div>
                <div class="value-case">
                  <div class="name-case" @click="chooseeACard(itemL.uid)">{{itemL.bankCardNumber}}</div>
                  <div class="right-arrow" @click="editBankCard(itemL.uid)">
                    <img src="@/assets/img/performance/personalCenter/rightarrow.png" alt>
                  </div>
                </div>
              </div>
            </div>
            <!-- 添加银行卡 固定 -->
            <div class="add-case">
              <div class="each-case">
                <div class="key-case" @click="goAdding">
                  <div class="img-case">
                    <img src="@/assets/img/public/goadd.png" alt>
                  </div>
                  <div class="key-name">银行卡账户</div>
                </div>
                <div class="value-case" @click="goAdding">
                  <div class="name-case" style="text-align: right;">添加</div>
                  <div class="right-arrow">
                    <img src="@/assets/img/performance/personalCenter/rightarrow.png" alt>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </van-popup>
  </div>
</template>
<script>
import Vue from "vue";
import { getuserUid } from "@/utils/auth.js";
import { mapMutations, mapState } from "vuex";
import { Dialog } from "vant";
Vue.use(Dialog);
export default {
  components: {},
  data() {
    return {
      title: "我的钱包",
      inputValue: 0,
      list: [],
      accountShow: false,
      bankInfo: 0,
      confirmDialog: false // 提现确定弹框
    };
  },
  computed: {
    ...mapState({
      walletWithdraw: state => state.walletWithdraw, // 获取我的钱包可提现余额
      bankCardObj: state => state.bankCardObj // 已经选择好的银行卡对象
    })
  },
  created() {
    this.bankList();
    if (this.bankCardObj == null) {
      this.bankInfo = 0;
    } else {
      this.bankInfo = 1;
    }
  },
  watch: {
    inputValue(newlval, oldval) {
      let walletWithdraw = this.walletWithdraw
      if(!newlval){
        return
      }
      if (newlval >= walletWithdraw) {
        this.inputValue = walletWithdraw;
      } else if (newlval < walletWithdraw) {
        // let testRule = /(^[0-9]{0,20}$)|(^[0-9]{0,20}[\.]{1}[0-9]{1,2}$)/
        let testRule = /^\d{1,9}(\.{0}|\.{1}\d{0,2})?$/
        if (!testRule.test(newlval)){
          this.inputValue = oldval
        } else  (
          this.inputValue = newlval
        )
        
      }
    }
  },
  methods: {
    handleKey(e) {
      // let i =/^((\d+\.\d*[1-9]\d{1})|(\d*[1-9]\d*\.\d{2}))$/
      // if (e.target.value >= this.walletWithdraw) {
      //   e.target.value = this.walletWithdraw;
      //   e.target.value = e.target.value.match(/^\d*(\.?\d{0,2})/g)[0] || null;
      // } else {
      //   e.target.value = e.target.value.match(/^\d*(\.?\d{0,2})/g)[0] || null;
      // }
    },
    // 金额聚焦
    inputFocus() {
      this.$refs.focusContent.focus();
    },
    diffBack() {
      this.$router.push("/performance/myWallet");
    },
    // 全部提现
    withdrawAll() {
      this.inputValue = this.walletWithdraw;
    },
    // 提成提交
    submitWithdraw() {
      if (this.bankCardObj == null) {
        this.$toast("提现账户无法为空");
        return false;
      } else if (this.inputValue <= 0) {
        this.$toast("提现金额需大于0哦~");
        return false;
      }
      // 调取接口 -- 提现成功 -- 跳转
      // this.confirmDialog  = true
      Dialog.confirm({
        title: "提现中...",
        message:
          "提现金额" +
          this.inputValue +
          "元，是否确定提现至银行账号" +
          this.bankCardObj.bankCardNumber +
          "？"
      })
        .then(() => {
          this.newCashOut();
        })
        .catch(() => {
          this.$toast("取消提现操作成功");
        });
    },
    // 选择账户
    chooseAccount() {
      this.accountShow = true;
    },
    // 选择一张银行卡
    chooseeACard(uid) {
      this.list.forEach(item => {
        if (item.uid == uid) {
          this.setBankCardObj(item);
        }
      });
      this.accountShow = false;
      if (this.bankCardObj == null) {
        this.bankInfo = 0;
      } else {
        this.bankInfo = 1;
      }
    },
    // 取消选择
    closeDialog() {
      this.accountShow = false;
    },
    async bankList() {
      let params = {
        userUid: getuserUid()
      };
      let bankResponse;
      try {
        bankResponse = await HTTP("app/user/getUserBankCards", params);
        if (bankResponse.code !== 200) {
          this.$toast(bankResponse.msg);
          return;
        }
        this.list = bankResponse.data;
        if (this.list.length == 0) {
          this.setBankCardObj();
        }
      } catch (error) {
        this.$toast("数据开小差了");
        console.log(error);
      }
    },
    // 提现功能
    async newCashOut() {
      let params = {
        userUid: getuserUid(),
        cashMoney: this.inputValue,
        withdrawalWay: 2,
        accountUid: this.bankCardObj.uid,
        realName: this.bankCardObj.realName,
        createBy: getuserUid()
      };
      let bankResponse;
      try {
        bankResponse = await HTTP("newCashOut", params);
        if (bankResponse.code !== 200) {
          this.$toast(bankResponse.msg);
          return;
        }
        const toast = this.$toast.success({
          duration: 2000, // 持续展示 toast
          forbidClick: true, // 禁用背景点击
          message: "提现成功，请耐心等待审核"
        });
        let second = 2;
        const timer = setInterval(() => {
          second--;
          if (!second) {
            clearInterval(timer);
            this.$toast.clear();
            this.$router.push("/performance/myWallet");
          }
        }, 1000);
      } catch (error) {
        this.$toast("数据开小差了");
        console.log(error);
      }
    },
    // 跳转添加新卡
    goAdding() {
      this.$router.push({
        name: "performance-bindAccount",
        params: { fromW: "newCard" }
      });
    },
    editBankCard(uid) {
      this.list.forEach(item => {
        if (item.uid == uid) {
          this.setBankCardObj(item);
        }
      });
      this.$router.push({
        name: "performance-bindAccount",
        params: { fromW: "oldCard" }
      });
    },
    ...mapMutations(["setBankCardObj"])
  }
};
</script>
<style lang="scss" scoped>
#for-deposit {
  position: absolute;
  width: 100%;
  height: 100%;
  background: #f9f9f9;
  .deposit-content {
    position: relative;
    width: 100%;
    height: 100%;
    .top-bg {
      width: 100%;
      background: #fff;
      border-bottom-left-radius: 0.5rem;
      border-bottom-right-radius: 0.5rem;
      .header {
        background: #5e8efe;
        color: #ffffff;
        height: 0.92rem;
        .title {
          color: #ffffff;
          font-size: 0.35rem;
        }
      }
    }
    .reset-wrapper {
      position: fixed;
      width: 100%;
      padding: 0.24rem;
      box-sizing: border-box;
      top: 0.92rem;
      left: 0;
      right: 0;
      overflow-y: scroll;
      bottom: 1.6rem;
      .reset-content {
        width: 100%;
        border-radius: 0.1rem;
        .reset-top {
          .top-content {
            width: 100%;
            box-sizing: border-box;
            padding: 0.36rem 0.24rem;
            border-bottom: 0.01rem solid #e5e5e5;
            border-radius: 0.1rem 0.1rem 0 0;
            .has-account {
              width: 100%;
              display: flex;
              justify-content: space-around;
              align-items: center;
              text-align: center;
              .img-case {
                width: 0.64rem;
                height: 0.64rem;
                margin-right: 0.2rem;
                img {
                  width: 0.64rem;
                  height: 0.64rem;
                }
              }
              .account-case {
                width: 3rem;
                flex: 1;
                color: #282828;
                font-size: 0.28rem;
                font-family: PingFangSC-Regular;
                text-align: left;
              }
              .name-case {
                width: 3rem;
                text-align: right;
                font-size: 0.28rem;
                color: #959595;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                margin-right: 0.2rem;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              }
              .right-case {
                width: 0.14rem;
                height: 0.26rem;
                img {
                  width: 0.14rem;
                  height: 0.26rem;
                }
              }
            }
            .no-account {
              position: relative;
              width: 100%;
              padding: 0 0.24rem;
              box-sizing: border-box;
              div {
                width: 50%;
                display: inline-block;
              }
              .tips-case {
                color: #282828;
                font-size: 0.28rem;
                font-family: PingFangSC-Regular;
                font-weight: 400;
              }
              .right-case {
                height: 0.26rem;
                color: #959595;
                font-size: 0.28rem;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                text-align: right;
                img {
                  width: 0.14rem;
                  height: 0.26rem;
                  margin-left: 0.2rem;
                }
              }
            }
          }
        }
        .reset-bottom {
          width: 100%;
          box-sizing: border-box;
          padding: 0 0.24rem;
          border-radius: 0 0 0.1rem 0.1rem;
          .bottom-top {
            padding: 0.5rem 0;
            border-bottom: 0.01rem solid #e5e5e5;
            .top-header {
              width: 100%;
              color: #656565;
              font-size: 0.28rem;
              font-family: PingFangSC-Regular;
              font-weight: 400;
              text-align: left;
            }
            .top-num {
              height: 100%;
              width: 100%;
              color: #5e8efe;
              font-family: PingFangSC-Semibold;
              font-weight: 600;
              font-size: 0.56rem;
              text-align: left;
              margin-top: 0.3rem;
              div {
                display: inline-block;
                width: 10%;
              }
              input {
                width: 87%;
                border: none;
              }
            }
          }
          .bottom-bottom {
            padding: 0.24rem 0 0.4rem;
            color: #656565;
            font-size: 0.24rem;
            font-family: PingFangSC-Regular;
            font-weight: 400;
            line-height: 0.4rem;
            .all-num {
              margin: 0.16rem 0 0.4rem;
              div {
                display: inline-block;
                font-size: 0.26rem;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                color: #656565;
              }
              .left-case {
                margin-right: 0.4rem;
              }
              .right-case {
                color: #5e8efe;
              }
            }
            .btn-case {
              height: 0.88rem;
              line-height: 0.88rem;
              border-radius: 0.1rem;
              background: #5e8efe;
              font-size: 0.26rem;
              font-family: PingFangSC-Semibold;
              font-weight: 600;
              color: #fff;
              text-align: center;
            }
          }
        }
      }
    }
  }
  .account-list {
    position: relative;
    height: 6.5rem;
    width: 100%;
    .header-case {
      width: 100%;
      position: fixed;
      top: 0;
      left: 0;
      height: 1rem;
      border-bottom: 0.01rem solid #e5e5e5;
      .mid-header {
        text-align: center;
        line-height: 1rem;
        color: #282828;
        font-size: 0.32rem;
        font-family: PingFangSC-Regular;
        font-weight: 400;
      }
      .close-btn {
        position: absolute;
        top: 0.32rem;
        right: 0.24rem;
        img {
          height: 0.36rem;
          width: 0.36rem;
        }
      }
    }
    .reset-content {
      position: fixed;
      width: 100%;
      box-sizing: border-box;
      top: 1rem;
      left: 0;
      right: 0;
      overflow-y: scroll;
      bottom: 0rem;
      .ali-wechat {
        .each-case {
          width: 100%;
          box-sizing: border-box;
          padding: 0.36rem 0.24rem;
          border-bottom: 0.01rem solid #e5e5e5;
          div {
            display: inline-block;
            height: 0.64rem;
            width: 50%;
          }
          .key-case {
            div {
              display: inline-block;
            }
            .img-case {
              width: 0.64rem;
              height: 0.64rem;
              margin-right: 0.2rem;
              img {
                width: 0.64rem;
                height: 0.64rem;
              }
            }
            .key-name {
              color: #282828;
              font-family: PingFangSC-Regular;
              font-weight: 400;
              height: 0.64rem;
              line-height: 0.64rem;
              vertical-align: top;
              font-size: 0.28rem;
            }
          }
          .value-case {
            position: relative;
            width: 50%;
            .name-case {
              width: 85%;
              height: 0.64rem;
              line-height: 0.64rem;
              word-wrap: normal;
              text-overflow: ellipsis;
              white-space: nowrap;
              overflow: hidden;
              font-size: 0.28rem;
              color: #959595;
              font-family: PingFangSC-Regular;
              font-weight: 400;
            }
            .right-arrow {
              position: absolute;
              width: 15%;
              height: 0.64rem;
              text-align: right;
              img {
                width: 0.14rem;
                height: 0.26rem;
                margin-top: 0.19rem;
              }
            }
          }
        }
      }
      .has-add {
        .has-case {
          .each-case {
            width: 100%;
            box-sizing: border-box;
            padding: 0.36rem 0.24rem;
            border-bottom: 0.01rem solid #e5e5e5;
            div {
              display: inline-block;
              height: 0.64rem;
              width: 50%;
            }
            .key-case {
              div {
                display: inline-block;
              }
              .img-case {
                width: 0.64rem;
                height: 0.64rem;
                margin-right: 0.2rem;
                img {
                  width: 0.64rem;
                  height: 0.64rem;
                }
              }
              .key-name {
                color: #282828;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                height: 0.64rem;
                line-height: 0.64rem;
                vertical-align: top;
                font-size: 0.28rem;
              }
            }
            .value-case {
              position: relative;
              width: 50%;
              .name-case {
                width: 75%;
                height: 0.64rem;
                line-height: 0.64rem;
                word-wrap: normal;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
                font-size: 0.28rem;
                color: #959595;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                text-align: right;
              }
              .right-arrow {
                position: absolute;
                width: 25%;
                height: 0.64rem;
                text-align: right;
                img {
                  width: 0.14rem;
                  height: 0.26rem;
                  margin-top: 0.19rem;
                }
              }
            }
          }
        }
        .add-case {
          .each-case {
            width: 100%;
            box-sizing: border-box;
            padding: 0.36rem 0.24rem;
            border-bottom: 0.01rem solid #e5e5e5;
            div {
              display: inline-block;
              height: 0.64rem;
              width: 50%;
            }
            .key-case {
              div {
                display: inline-block;
              }
              .img-case {
                width: 0.64rem;
                height: 0.64rem;
                margin-right: 0.2rem;
                img {
                  width: 0.64rem;
                  height: 0.64rem;
                }
              }
              .key-name {
                color: #282828;
                font-family: PingFangSC-Regular;
                font-weight: 400;
                height: 0.64rem;
                line-height: 0.64rem;
                vertical-align: top;
                font-size: 0.28rem;
              }
            }
            .value-case {
              position: relative;
              width: 50%;
              .name-case {
                width: 75%;
                height: 0.64rem;
                line-height: 0.64rem;
                word-wrap: normal;
                text-overflow: ellipsis;
                white-space: nowrap;
                overflow: hidden;
                font-size: 0.28rem;
                color: #959595;
                font-family: PingFangSC-Regular;
                font-weight: 400;
              }
              .right-arrow {
                position: absolute;
                width: 25%;
                height: 0.64rem;
                text-align: right;
                img {
                  width: 0.14rem;
                  height: 0.26rem;
                  margin-top: 0.19rem;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>
